/*
 *  Copyright © 2016-2018, Turing Technologies, an unincorporated organisation of Wynne Plaga
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.turingtechnologies.materialscrollbardemo;

import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class Handle extends Component implements Component.DrawTask, Component.EstimateSizeListener {
    // 圆环宽度
    private static final float CIRCLE_STROKE_WIDTH = 1f;
    private RectFloat handleArc;
    private Arc arc;
    private RectFloat handleHold;

    // 绘制圆环的画笔
    private Paint circlePaint;

    private boolean isStateMax = false;

    /**
     * Hand*
     * @param context context
     */
    public Handle(Context context) {
        super(context);
        addDrawTask(this);
    }

    /**
     * drawMax
     */
    public void drawMax() {
        isStateMax = true;
        invalidate();
    }

    /**
     * drawMin
     */
    public void drawMin() {
        isStateMax = false;
        invalidate();
    }

    private void initPaint() {
        circlePaint = new Paint();
        circlePaint.setColor(new Color(Color.getIntColor("#ff0099")));
        circlePaint.setStrokeWidth(CIRCLE_STROKE_WIDTH);
        circlePaint.setAntiAlias(true);
        handleArc = new RectFloat(5, getTop(), getWidth(), getBottom());
        arc = new Arc(90, 180, true);
        handleHold = new RectFloat((float)getWidth() / 2, (float)getTop(), (float)getWidth(), (float)getBottom());
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        // 初始化画笔
        initPaint();
        handleHold.left = 0;
        if (!isStateMax) {
            handleHold.left = (float)getWidth() / 2;
            canvas.drawArc(handleArc, arc, circlePaint);
        }
        canvas.drawRect(handleHold, circlePaint);
    }

    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        int width = Component.EstimateSpec.getSize(widthEstimateConfig);
        int height = Component.EstimateSpec.getSize(heightEstimateConfig);
        setEstimatedSize(
                Component.EstimateSpec.getChildSizeWithMode(width, width, Component.EstimateSpec.PRECISE),
                Component.EstimateSpec.getChildSizeWithMode(height, height, Component.EstimateSpec.PRECISE)
        );
        return true;
    }
}
